package com.picshare.config;

public class ServerConfig {
    private String serverIp = "localhost";
    private int serverPort = 9500;
    
    // 单例模式
    private static ServerConfig instance;
    
    private ServerConfig() {}
    
    public static synchronized ServerConfig getInstance() {
        if (instance == null) {
            instance = new ServerConfig();
            instance.loadConfig();
        }
        return instance;
    }
    
    // 加载配置（这里从文件加载，实际项目中可以从数据库或其他配置源）
    private void loadConfig() {
        try {
            java.io.File configFile = new java.io.File("./config/server.properties");
            if (configFile.exists()) {
                java.util.Properties props = new java.util.Properties();
                props.load(new java.io.FileInputStream(configFile));
                this.serverIp = props.getProperty("server.ip", "localhost");
                this.serverPort = Integer.parseInt(props.getProperty("server.port", "9500"));
            }
        } catch (Exception e) {
            // 使用默认值
            System.out.println("加载服务器配置失败，使用默认值: " + e.getMessage());
        }
    }
    
    // 保存配置
    public void saveConfig() {
        try {
            java.io.File configDir = new java.io.File("./config");
            if (!configDir.exists()) {
                configDir.mkdirs();
            }
            
            java.util.Properties props = new java.util.Properties();
            props.setProperty("server.ip", this.serverIp);
            props.setProperty("server.port", String.valueOf(this.serverPort));
            
            props.store(new java.io.FileOutputStream("./config/server.properties"), "Server Configuration");
        } catch (Exception e) {
            System.err.println("保存服务器配置失败: " + e.getMessage());
        }
    }
    
    // 获取完整的URL
    public String getServerUrl() {
        return "http://" + serverIp + ":" + serverPort;
    }
    
    // Getters and Setters
    public String getServerIp() {
        return serverIp;
    }
    
    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }
    
    public int getServerPort() {
        return serverPort;
    }
    
    public void setServerPort(int serverPort) {
        this.serverPort = serverPort;
    }
}